import syscall

fn main():void! {
    var pid = syscall.fork() catch err {
        return print('fork err: ', err.msg(), '\n')
    }

    if pid > 0 {
        // 主进程, wait 阻塞, 并读取返回值
        var (result, status) = syscall.wait(pid, 0)
        print('wait status: ', status, '\n')
    } else {
        // 子进程通过 exec 调用 sleep
        syscall.exec('/bin/sleep', ['sleep', '1'], ['env1=v', 'env2=2'])
    }


    // 测试主动 kill
    pid = syscall.fork() catch err {
        return print('fork err: ', err.msg(), '\n')
    }

    if pid > 0 {
        // 主进程, wait 阻塞, 并读取返回值
        var has_err = false
        syscall.kill(pid, syscall.SIGKILL) catch err {
            print('kill err: ', err.msg(), '\n')
            has_err = true
        }

        if !has_err {
            println('kill success')
        }
    } else {
        syscall.exec('/bin/sleep', ['sleep', '1000'], [])
    }
}